package com.xs.ahoi.hcxs.utils;

import java.util.Iterator;
import java.util.regex.Pattern;

public class StringUtils {
    public static final String SPACE = " ";
    public static final String EMPTY = "";
    public static final String LF = "\n";
    public static final String CR = "\r";
    public static final int INDEX_NOT_FOUND = -1;
    private static final int PAD_LIMIT = 8192;
    private static final Pattern WHITESPACE_PATTERN = Pattern.compile("(?: |\\u00A0|\\s|[\\s&&[^ ]])\\s*");

    public StringUtils() {
    }

    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    public static boolean isNotEmpty(CharSequence cs) {
        return !isEmpty(cs);
    }

    public static String join(Iterable<?> iterable, String separator) {
        return iterable == null ? null : join(iterable.iterator(), separator);
    }


    public static String join(Iterator<?> iterator, String separator) {
        if (iterator == null) {
            return null;
        } else if (!iterator.hasNext()) {
            return "";
        } else {
            Object first = iterator.next();
            if (!iterator.hasNext()) {
                String result = ObjectUtils.toString(first);
                return result;
            } else {
                StringBuilder buf = new StringBuilder(256);
                if (first != null) {
                    buf.append(first);
                }

                while (iterator.hasNext()) {
                    if (separator != null) {
                        buf.append(separator);
                    }

                    Object obj = iterator.next();
                    if (obj != null) {
                        buf.append(obj);
                    }
                }

                return buf.toString();
            }
        }
    }

    public static String join(Object[] array, String separator) {
        return array == null ? null : join(array, separator, 0, array.length);
    }

    public static String join(Object[] array, String separator, int startIndex, int endIndex) {
        if (array == null) {
            return null;
        } else {
            if (separator == null) {
                separator = "";
            }

            int noOfItems = endIndex - startIndex;
            if (noOfItems <= 0) {
                return "";
            } else {
                StringBuilder buf = new StringBuilder(noOfItems * 16);

                for (int i = startIndex; i < endIndex; ++i) {
                    if (i > startIndex) {
                        buf.append(separator);
                    }

                    if (array[i] != null) {
                        buf.append(array[i]);
                    }
                }

                return buf.toString();
            }
        }
    }

    public static String replaceWrap(String str) {
        if (isNotEmpty(str)) {
            return str.replace("\r\n", "").replace("\n", "").replace("\r", "");
        }
        return "";
    }

    // 判断是否乱码
    public static boolean isErrorCode(String str) {
        String regEx = "[\u4e00-\u9fa5]|[0-9]|[a-z]|[A-Z]|，|。|！|；|：|“|”| ";
        return !regMatch(str, regEx);
    }

    // 获取进度
    public static String getPercent(int position, long totalLenth) {
        String percent;
        if (totalLenth == 0) {
            percent = "0.00%";
        } else {
            percent = ObjectUtils.toFix(position * 100f / totalLenth, 2) + "%";
        }
        return percent;
    }

    // 正则匹配
    public static boolean regMatch(String str, String reg) {
        Pattern pattern = Pattern.compile(reg);
        return regMatch(pattern, str);
    }

    // 正则匹配
    public static boolean regMatch(Pattern pattern, String str) {
        return pattern.matcher(str).find();
    }
}
